//
//  BranchSearchCell.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/11/29.
//

import Foundation
import UIKit

class BranchSearchCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: DeptModal) {
        iconL.text = "\(modal.deptName!.prefix(1))"
        iconL.backgroundColor = modal.color
        nameL.text = modal.deptName
        
        yyL.text = modal.sfyy == true ? "营业" : "不营业"
        xwdL.isHidden = modal.sfxz == false
    }
    
    func setupUI() {
        addSubview(iconL)
        iconL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(25)
            make.width.height.equalTo(40)
        }
        
        addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(iconL.snp.right).offset(18)
        }
        
        addSubview(yyL)
        yyL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-75)
        }
        
        xwdL.isHidden = true
        addSubview(xwdL)
        xwdL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-45)
        }
        
        addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.bottom.equalToSuperview()
            make.left.equalTo(nameL.snp.left)
            make.right.equalTo(self.snp.right).offset(-24)
            make.height.equalTo(0.5)
        }
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        if highlighted {
            layer.opacity = 0.6
        } else {
            layer.opacity = 1.0
        }
    }
    
    lazy var iconL: UILabel = {
        let l = UILabel()
        l.font = .systemFont(ofSize: 18)
        l.textColor = .white
        l.textAlignment = .center
        l.layer.cornerRadius = 20
        l.layer.masksToBounds = true
        return l
    }()
    
    lazy var nameL: UILabel = {
        let l = UILabel()
        l.font = .systemFont(ofSize: 16)
        l.textColor = .hex("#333333")
        return l
    }()
    
    lazy var yyL: UILabel = {
        let label = UILabel()
        label.text = ""
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = .primary
        return label
    }()
    
    lazy var xwdL: PaddingLabel = {
        let label = PaddingLabel()
        label.text = "新"
        label.insets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = .white
        label.layer.cornerRadius = 6
        label.backgroundColor = .hex("#FF0000")
        label.layer.masksToBounds = true
        return label
    }()
    
    lazy var line: UIView = {
        let li = UIView()
        li.backgroundColor = .hex("#E5E5E5")
        return li
    }()
}
